package com.yunfatong.evidence.service;

import com.yunfatong.evidence.common.exception.ValidationException;
import com.yunfatong.evidence.common.resultEnum.ResultCode;
import com.yunfatong.evidence.form.common.BaseApplyForm;
import com.yunfatong.evidence.form.contract.BaseContractForm;
import com.yunfatong.evidence.mapper.UserMasterMapperCustom;
import com.yunfatong.evidence.model.UserMaster;
import com.yunfatong.evidence.model.UserMasterExample;
import com.yunfatong.evidence.model.UserSlave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 大客户service
 */
@Service
public class UserMasterService {

    @Autowired
    private UserMasterMapperCustom userMasterMapperCustom;

    @Autowired
    private UserSlaveService userSlaveService;


    public List<UserMaster> getMasterList() {
        UserMasterExample example = new UserMasterExample();
        System.out.println(Thread.currentThread().getName());

        userSlaveService.insertUserMaster();
        return userMasterMapperCustom.selectByExample(example);
    }

    @Transactional(rollbackFor = ArithmeticException.class, propagation = Propagation.REQUIRES_NEW)
    public void insertUserMaster() {
        UserMaster record = new UserMaster();
        record.setRemark("1");
        record.setStaffInfoId(1L);
        record.setUserMasterCode("1");
        record.setCreateTime(new Date());
        record.setUserMasterName("1");
        System.out.println(Thread.currentThread().getName());
        userMasterMapperCustom.insert(record);
        int i = 1 / 0;
    }


    /**
     * 公共根据code 查询id并且修改请求实体中的id,
     *
     * @param applyForm
     * @auhtor liran
     */
    public void commonGetUserIdByCode(BaseApplyForm<? extends BaseContractForm> applyForm, Class<?> clazz) {
        String getUserMasterCode = "getUserMasterCode";
        String getUserSlaveCode = "getUserSlaveCode";
        String setUserMasterId = "setUserMasterId";
        String setUserSlaveId = "setUserSlaveId";
        String getUserMasterId = "getUserMasterId";
        String getUserSlaveId = "getUserSlaveId";
        Object param = applyForm.getApplyInfo();
        if (applyForm != null && param != null) {
            //获取get code 方法
            Method methodMasterCode = ReflectionUtils.findMethod(clazz, getUserMasterCode);
            Method methodSlaveCode = ReflectionUtils.findMethod(clazz, getUserSlaveCode);
            //获取get id 方法
            Method methodGetMasterId = ReflectionUtils.findMethod(clazz, getUserMasterId);
            Method methodGetSlaveId = ReflectionUtils.findMethod(clazz, getUserSlaveId);
            //获取set方法
            Method methodMasterId = ReflectionUtils.findMethod(clazz, setUserMasterId, Long.class);
            Method methodSlaveId = ReflectionUtils.findMethod(clazz, setUserSlaveId, Long.class);


            //把原来的参数 userSlaveId,userMasterId 设置成0
//  ReflectionUtils.invokeMethod(methodMasterId, param, 0L);
// ReflectionUtils.invokeMethod(methodSlaveId, param, 0L);

            String masterCode = (String) ReflectionUtils.invokeMethod(methodMasterCode, param);
            String slaveCode = (String) ReflectionUtils.invokeMethod(methodSlaveCode, param);

            //获取方法中id参数不允许传过来
            Object getMasterId = ReflectionUtils.invokeMethod(methodGetMasterId, param);
            Object getSlaveId = ReflectionUtils.invokeMethod(methodGetSlaveId, param);
            if (null != getMasterId || null != getSlaveId) {
                throw new ValidationException(ResultCode.ARGUMENTS_ERROR, "UserMasterId和UserSlaveId不是请求参数");
            }

            UserSlave slave = userSlaveService.selectSlaveUserInfoBySlaveCode(slaveCode, masterCode);
            if (null != slave && null != slave.getUserMasterId() && null != slave.getId()) {
                ReflectionUtils.invokeMethod(methodMasterId, param, slave.getUserMasterId());
                ReflectionUtils.invokeMethod(methodSlaveId, param, slave.getId());
            }

        }
    }


}
